package wmhsudoku.board;

import java.util.Arrays;
import java.util.Scanner;
import java.util.Vector;
import java.awt.Point;
import java.io.*;

public class Map {
	
	private int[][] grid;
	private int size;	//zakladam,ze mapa jest kwadratowa,ale nie problem to zmienic
	
	private Point highlighted = null;
	
	public Map() { this(9); }
	public Map(int rozmiar) {
		size = rozmiar;
		grid = new int[rozmiar][rozmiar];
	}
	
	private int[][] clone2DArray(int[][] array) {
	    int rows=array.length ;

	    //clone the 'shallow' structure of array
	    int[][] newArray =(int[][]) array.clone();
	    //clone the 'deep' structure of array
	    for(int row=0;row<rows;row++){
	        newArray[row]=(int[]) array[row].clone();
	    }

	    return newArray;
	}
	
	
	public Map(Map map) {
		this.grid = clone2DArray(map.getGrid());
	}
	
	public void setHighlight(Point p) {
		highlighted = new Point(p);
	}
	
	public Point getHighlight() {
		return highlighted;
	}
	
	//Konstruktor, ktory wczytuje mape i umiejscowienie elementow z pliku
	public Map(String f) {
		int x=0, y=0;
		Vector<Integer> v = new Vector<Integer>(1);
		try {	
			File file = new File(f);
			Scanner scanner = new Scanner(file);
			while (scanner.hasNextLine()){
				Scanner sl = new Scanner(scanner.nextLine());
		        while (sl.hasNext()) {
		        	if (x==0) {
		        		v.add(sl.nextInt());
		        	} else {
		        		grid[x][y++] = sl.nextInt();
		        	}	
		        }
		        sl.close();	
		        if (x==0) {
		        	grid = new int[v.size()][v.size()];
		        	size = v.size();
		        	for (int i = 0; i<v.size(); i++) {
		        		grid[0][i]= v.get(i);
		        	}
		        }
		        x++;
		        y=0;
			}
			scanner.close();
		} catch (FileNotFoundException e) {
		    e.printStackTrace();
		}
	}

	public int getSize() {
		return size;
	}
	public int getField(int x, int y) {
		return grid[y][x];
	}
	public int getField(int[] coord) {
		return grid[coord[1]][coord[0]];
	}
	public int getField(Point coord) {
		return grid[(int)coord.getY()][(int)coord.getX()];
	}
	public int[] getRow(int y) {
		return grid[y];
	}
	public int[][] getGrid() {
		return grid;
	}	

	public void setField(int x, int y, int value) {
		grid[y][x] = value;
	}

	public void setField(Point p, int value) {
		grid[(int)p.getY()][(int)p.getX()] = value;
	}
	public void setField(int[] coord, int value) {
		grid[coord[1]][coord[0]] = value;
	}
	public void setGrid(int[][] g) {
		grid = g;
	}
	
	public boolean outOfMap(int x, int y) {
		if (x < 0 || x >= getSize() || y < 0 || y >= getSize())
			return true;
		else
			return false;
	}
	
	public void print() {
		
		System.out.println(Arrays.deepToString(getGrid()));
	}
	public void print2() {
		int i = 0;
		int j = 0;
		while (i < getSize()) {
			while (j < getSize()) {
				System.out.print(getField(i, j)+" ");
				++j;
			}
			System.out.print("\n");
			++i;
			j=0;
		}
	}

	public void createRandomMap() {
	
	}	
	
	public void updateSudoku(Point[] newPoints, int[] newValues) {
		int i=0;
		for(Point p : newPoints) {
			setField(p, newValues[i++]);
		}
	}
	
}
